package jc.malpractice.db;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.text.SimpleDateFormat;

public class Help {
    public static JSONArray Result1Json(ResultSet resultSet){
        try{
            // 获取列数
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            // 遍历ResultSet中的每条数据
            JSONArray array = new JSONArray();
            while (resultSet.next()) {
                JSONObject jsonObj = new JSONObject();

                // 遍历每一列
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnLabel(i);
                    Object value = resultSet.getObject(columnName);
                    jsonObj.set(columnName,value);
                }
                array.add(jsonObj);
            }



            resultSet.close();
            return array;

        }catch (Exception ex){

        }
        return  null;
    }
    public static JSONObject Result2Json(ResultSet resultSet){
        try{
            // 获取列数
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            // 遍历ResultSet中的每条数据
            JSONArray array = new JSONArray();
            while (resultSet.next()) {
                JSONObject jsonObj = new JSONObject();

                // 遍历每一列
                for (int i = 1; i <= columnCount; i++) {

                    String columnName = metaData.getColumnLabel(i);
                    Object value = resultSet.getObject(columnName);
                    jsonObj.set(columnName,value);

                }
                jsonObj.set("event_time",resultSet.getString("event_time"));
                array.add(jsonObj);
                System.out.println(jsonObj.toJSONString(2));
            }
            JSONObject result = new JSONObject();
            result.set("data",array);
            System.out.println(result.toJSONString(2));

            resultSet.close();
            return result;

        }catch (Exception ex){

        }
        return  null;
    }
    // 用户登录
    public static JSONObject Result3Json(ResultSet resultSet){
        try{
            // 获取列数
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            // 遍历ResultSet中的每条数据
            JSONArray array = new JSONArray();
            while (resultSet.next()) {
                JSONObject jsonObj = new JSONObject();

                // 遍历每一列
                for (int i = 1; i <= columnCount; i++) {

                    String columnName = metaData.getColumnLabel(i);
                    Object value = resultSet.getObject(columnName);
                    jsonObj.set(columnName,value);

                }
                array.add(jsonObj);
                System.out.println(jsonObj.toJSONString(2));
            }
            JSONObject result = new JSONObject();
//            if (array.isEmpty()){
//                array.add("{\"username\":\"null\",\"password\":\"null\"}");
//            }
            result.set("data",array);
            result.set("code",0);
            result.set("msg","登入成功");
            System.out.println(result.toJSONString(2));

            resultSet.close();
            return result;

        }catch (Exception ex){

        }
        return  null;
    }
    public static JSONObject Result4Json(ResultSet resultSet){
        try{
            // 获取列数
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            // 遍历ResultSet中的每条数据
            JSONArray array = new JSONArray();
            while (resultSet.next()) {
                JSONObject jsonObj = new JSONObject();

                // 遍历每一列
                for (int i = 1; i <= columnCount; i++) {

                    String columnName = metaData.getColumnLabel(i);
                    Object value = resultSet.getObject(columnName);
                    jsonObj.set(columnName,value);

                }
                //转化时间
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                System.out.println(resultSet.getString("report_time"));
                System.out.println(resultSet.getDate("report_time"));
                //System.out.println(resultSet.getString("report_time") instanceof String);
                //long str = Long.valueOf("string");
                //System.out.println(str);
                String str = resultSet.getString("report_time");
                System.out.println(str);
                jsonObj.set("report_time",resultSet.getString("report_time"));
                jsonObj.set("update_time",resultSet.getString("update_time"));
                array.add(jsonObj);
                System.out.println(jsonObj.toJSONString(2));
            }
            JSONObject result = new JSONObject();
//            if (array.isEmpty()){
//                array.add("{\"username\":\"null\",\"password\":\"null\"}");
//            }
            result.set("data",array);
            result.set("code",0);
            result.set("msg","返回成功");
            System.out.println(result.toJSONString(2));

            resultSet.close();
            return result;

        }catch (Exception ex){

        }
        return  null;
    }

    public static JSONObject Result5Json(ResultSet resultSet){
        try{
            // 获取列数
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            // 遍历ResultSet中的每条数据
            JSONArray array = new JSONArray();
            while (resultSet.next()) {
                JSONObject jsonObj = new JSONObject();

                // 遍历每一列
                for (int i = 1; i <= columnCount; i++) {

                    String columnName = metaData.getColumnLabel(i);
                    Object value = resultSet.getObject(columnName);
                    jsonObj.set(columnName,value);

                }
                //设置属性
                if(jsonObj.getInt("is_admin") == 1){
                    jsonObj.set("is_admin","是");
                } else{
                    jsonObj.set("is_admin","否");
                }
                System.out.println(jsonObj.get("is_admin"));
                array.add(jsonObj);
            }
            JSONObject result = new JSONObject();
//            if (array.isEmpty()){
//                array.add("{\"username\":\"null\",\"password\":\"null\"}");
//            }
            result.set("data",array);
            result.set("code",0);
            result.set("msg","登入成功");
            System.out.println(result.toJSONString(2));

            resultSet.close();
            return result;

        }catch (Exception ex){

        }
        return  null;
    }

    //把result数据转jsonarray
    public static JSONObject Result6Json(ResultSet resultSet){
        try{
            // 获取列数
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            // 遍历ResultSet中的每条数据
            JSONArray array = new JSONArray();
            while (resultSet.next()) {
                JSONObject jsonObj = new JSONObject();

                // 遍历每一列
                for (int i = 1; i <= columnCount; i++) {

                    String columnName = metaData.getColumnLabel(i);
                    Object value = resultSet.getObject(columnName);
                    jsonObj.set(columnName,value);

                }
                array.add(jsonObj);
                System.out.println(jsonObj.toJSONString(2));
            }
            JSONObject result = new JSONObject();
            result.set("data",array);
            System.out.println(result.toJSONString(2));

            resultSet.close();
            return result;

        }catch (Exception ex){

        }
        return  null;
    }

    public static JSONObject Result7Json(ResultSet resultSet){
        try{
            // 获取列数
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            // 遍历ResultSet中的每条数据
            JSONArray array = new JSONArray();
            while (resultSet.next()) {
                JSONObject jsonObj = new JSONObject();

                // 遍历每一列
                for (int i = 1; i <= columnCount; i++) {

                    String columnName = metaData.getColumnLabel(i);
                    Object value = resultSet.getObject(columnName);
                    jsonObj.set(columnName,value);

                }
                array.add(jsonObj);
                System.out.println(jsonObj.toJSONString(2));
            }
            JSONObject result = new JSONObject();
            result.set("data",array);
            System.out.println(result.toJSONString(2));

            resultSet.close();
            return result;

        }catch (Exception ex){

        }
        return  null;
    }
}
